This page is about trend in personal consumption in the US

TODO: Add labels/titles;

Change colors;

Calculate percentage; Add animated Radar Area Chart;

Add description.

Import data

consumption_product = readxl::read_excel("data/consumption_product.xlsx") %>% 
  janitor::clean_names() %>% 
  pivot_longer(
    x2019_q1 : x2021_q3,
    names_to = "time",
    names_prefix = "x",
    values_to = "consumption"
  ) 

consumption_function = readxl::read_excel("./data/consumption_function.xlsx") %>% 
  janitor::clean_names() %>% 
  filter(as.numeric(line) <= 28) %>% 
    pivot_longer(
    x2019_q1 : x2021_q3,
    names_to = "time",
    names_prefix = "x",
    values_to = "consumption"
  ) %>% 
  mutate(functions = recode(functions, `Household consumption expenditures (for services)` = "household",
                           `Final consumption expenditures of nonprofit institutions serving households (NPISHs)1` = "nonprofit consumption"))

general_1 = consumption_function %>% 
  filter(functions %in% c("Goods","Services"))

covid_seasonal = read_csv("covid_seasonal.csv") %>% 
  rename(time = date) %>% 
  select(time, quarterly)

consumption_seasonal = general_1 %>% 
  select(-line)

covid_consumption = left_join(consumption_seasonal, covid_seasonal, by = "time")

General

joint_plot = plot_ly(covid_consumption, x = ~time) %>% 
  add_trace(y = ~consumption, type = "scatter", mode = "lines", color = ~functions, yaixs = "y") %>% 
  add_trace(y = ~quarterly, type = "bar", name = "Covid Cases", yaxis = "y2")  %>% 
  layout(yaxis=list(title = "consumption expenditure", side="left"),
         yaxis2=list(title = "covid cases", side="right",overlaying="y"),
         showlegend=TRUE)

joint_plot

2nd level

general_2 = consumption_function %>% 
  filter(functions %in% c("Durable goods","Nondurable goods","household","nonprofit consumption")) %>% 
  select(-line) %>% 
  pivot_wider(names_from = functions, values_from = consumption) %>% 
  janitor::clean_names()

subfig_1 = plot_ly(general_2, x = ~time, y = ~durable_goods, type = "bar", name = "Durable Goods") %>% 
  add_trace(y = ~nondurable_goods, name = "Nondurable Goods") %>% 
  layout(yaxis = list(title = "Consumption"), barmode = "stack")

subfig_2 = plot_ly(general_2, x = ~time, y = ~household, type = "bar", name = "Household", colors = "Dark2") %>% 
  add_trace(y = ~nonprofit_consumption, name = "Nonprofit Consumption", colors = "Dark2") %>% 
  layout(yaixs = list(title = "Consumption"), barmode = "stack")

subfig_1
subfig_2

3rd level

durable_goods = 
  consumption_function %>% 
  filter(functions %in% c("Motor vehicles and parts","Furnishings and durable household equipment","Recreational goods and vehicles","Other durable goods")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h', x = 0, y = -0.2))

nondurable_goods = 
  consumption_function %>% 
  filter(functions %in% c("Food and beverages purchased for off-premises consumption","Clothing and footwear","Gasoline and other energy goods","Other nondurable goods")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h', x = 0, y = -0.2))

household_consumption =
  consumption_function %>% 
  filter(functions %in% c("Housing and utilities","Health care","Transportation services","Recreation services","Food services and accommodations","Financial services and insurance","Other services")) %>% 
  plot_ly(x = ~time, y = ~consumption, type = 'scatter', mode = 'lines', yaxis="y", color = ~functions) %>% 
  layout(legend = list(orientation = 'h', x = 0, y = -0.2))

durable_goods
nondurable_goods
household_consumption

radar charts (TODO)

fig <- plot_ly(
    type = 'scatterpolar',
    fill = 'toself'
  ) 
fig <- fig %>%
  add_trace(
    r = c(39, 28, 8, 7, 28, 39),
    theta = c('A','B','C', 'D', 'E', 'A'),
    name = 'Group A'
  ) 
fig <- fig %>%
  add_trace(
    r = c(1.5, 10, 39, 31, 15, 1.5),
    theta = c('A','B','C', 'D', 'E', 'A'),
    name = 'Group B'
  ) 
fig <- fig %>%
  layout(
    polar = list(
      radialaxis = list(
        visible = T,
        range = c(0,50)
      )
    )
  )

fig